package com.bw.usercenter.ui

import kotlinx.coroutines.*
import okhttp3.Dispatcher
import kotlin.coroutines.coroutineContext
import kotlin.system.measureTimeMillis


fun main(){
    run7()
}

//练习
fun a(){
            GlobalScope.launch {
            delay(1000L)
            doWord()
            println("666")

        }
        println("123")
        Thread.sleep(2000L)
//    堵塞了主线程2秒


        val job = GlobalScope.launch {
            delay(1000L)
        }
        println("123")
//        job.join()//等待子协程结束
}
//挂起函数
suspend fun doWord(){
    delay(1000L)
    println("wwww")
}

fun run1(){
    runBlocking {
        val job = launch {
            repeat(1000){
                println("current car $it")
                delay(500L)
            }
        }
        delay(1300L)
//            job.cancel()
//            job.join()
        //取消
        job.cancelAndJoin()//cancel()join()可以不同写
        println("this task is stop.......")
    }
}

/**
 * 协作
 * i<5会取消完在执行   isActive直接取消作业
 */
fun run2(){
    runBlocking {
        //当前时间
        val startTime= System.currentTimeMillis()
        //调度违约
        val job = launch(Dispatchers.Default) {
            var nextPrintTime = startTime
            var i = 0
            while (isActive){//i<5会取消完在执行   isActive直接取消作业
                if (System.currentTimeMillis()>=nextPrintTime){
                    println("job ； sleeping ${i++}")
                    nextPrintTime+=500L
                }
            }
        }
        delay(1300L)
        println("等待一段时间")
        job.cancelAndJoin()
        println("取消一个作业，并等会他结束")
    }
}

/**
 * 运行时不能取消代码块
 */
fun run3(){
    runBlocking {
        val job = launch {
            try {
                repeat(10){
                    println("job---Im seeping:$it")
                    print(1000L)
                }
            } finally {
                println("jog ----finally")
            }
        }
        delay(1300L)
        println("main执行完")
        job.cancelAndJoin()
        println("取消了")
    }
}
/**
 * 超时
 */
suspend fun run4(){
    val result = withTimeoutOrNull(1300L){
        repeat(10){
            println("I m seeping:$it")
            delay(500L)
        }
    }
    println("Request is $result")

}
/**
 * 组合挂起函数
 */
suspend fun run5(){
    //在main里执行
    val timee = measureTimeMillis {
        val one = doOne()
        val tow = doTow()
        println("$one-----$tow")
    }
    println("-----$timee")
}
suspend fun doOne():Int{
    delay(1000L)
    return 12
}
suspend fun doTow():Int{
    delay(1000L)
    return 15
}
/**
 * async并发
 */
fun run6(){
    runBlocking {
        val time = measureTimeMillis {
            val one = async { doOne() }
            val tow = async { doTow() }
            one.await()
            tow.await()
        }
        println("-----$time")
    }
}
/**
 * 惰性启动async
 */
fun run7(){
    runBlocking {
        val time = measureTimeMillis {
            val one = async(start = CoroutineStart.LAZY) { doOne() }
            val tow = async(start = CoroutineStart.LAZY) { doTow() }
            one.start()
            tow.start()
            println("The answer is ${one.await() + tow.await()}")
        }
        println("Cpmpleted-----$time")
    }
}



